package com.siwei.mes.service.mixProportion.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.entity.mixproportion.MixProportion;
import com.siwei.mes.entity.mixproportion.MixProportionUpdateLog;
import com.siwei.mes.mapper.mixProportion.MixProportionUpdateLogMapper;
import com.siwei.mes.service.mixProportion.MixProportionUpdateLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 配合比修改记录（品控）(MixProportionUpdateLog)表服务实现类
 *
 * @author linzi
 * @since 2024-09-07 23:59:46
 */
@Service
public class MixProportionUpdateLogServiceImpl implements MixProportionUpdateLogService {
    @Resource
    private MixProportionUpdateLogMapper mixProportionUpdateLogMapper;

    @Override
    public PageInfo<MixProportionUpdateLog> getPage(PageQuery<MixProportionUpdateLog, MixProportionUpdateLog> query) {
        MixProportionUpdateLog searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> mixProportionUpdateLogMapper.getList(searchParam));
    }

    @Override
    public MixProportionUpdateLog loadById(Long id) {
        return mixProportionUpdateLogMapper.loadById(id);
    }

    @Override
    public List<MixProportionUpdateLog> getAll(MixProportionUpdateLog entity) {
        return mixProportionUpdateLogMapper.getList(entity);
    }

    @Override
    public void insertLog(Long mixId, MixProportion beforeInfo, MixProportion afterInfo) {
        MixProportionUpdateLog updateLog = new MixProportionUpdateLog();
        updateLog.setMixId(mixId);
        updateLog.setBeforeJson(beforeInfo == null ? null : JsonUtils.beanToObject(beforeInfo));
        updateLog.setAfterJson(afterInfo == null ? null : JsonUtils.beanToObject(afterInfo));
        updateLog.setCreater(StpUtil.isLogin() ? StpUtil.getLoginIdAsLong() : null);
        updateLog.setCreateTime(new Date());
        mixProportionUpdateLogMapper.insertSelective(updateLog);
    }
}
